﻿namespace Chain.BLL
{
    using Chain.DAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class MemLevel
    {
        private readonly Chain.DAL.MemLevel dal = new Chain.DAL.MemLevel();

        public int Add(Chain.Model.MemLevel model)
        {
            if (this.Exists(-1, model.LevelPoint))
            {
                return -1;
            }
            return this.dal.Add(model);
        }

        public List<Chain.Model.MemLevel> DataTableToList(DataTable dt)
        {
            List<Chain.Model.MemLevel> list = new List<Chain.Model.MemLevel>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.MemLevel item = new Chain.Model.MemLevel();
                    if ((dt.Rows[i]["LevelID"] != null) && (dt.Rows[i]["LevelID"].ToString() != ""))
                    {
                        item.LevelID = int.Parse(dt.Rows[i]["LevelID"].ToString());
                    }
                    if ((dt.Rows[i]["LevelName"] != null) && (dt.Rows[i]["LevelName"].ToString() != ""))
                    {
                        item.LevelName = dt.Rows[i]["LevelName"].ToString();
                    }
                    if ((dt.Rows[i]["LevelPoint"] != null) && (dt.Rows[i]["LevelPoint"].ToString() != ""))
                    {
                        item.LevelPoint = int.Parse(dt.Rows[i]["LevelPoint"].ToString());
                    }
                    if ((dt.Rows[i]["ClassDiscountPercent"] != null) && (dt.Rows[i]["ClassDiscountPercent"].ToString() != ""))
                    {
                        item.LevelDiscountPercent = Convert.ToDecimal(dt.Rows[i]["ClassDiscountPercent"]);
                    }
                    if ((dt.Rows[i]["ClassPointPercent"] != null) && (dt.Rows[i]["ClassPointPercent"].ToString() != ""))
                    {
                        item.LevelPointPercent = Convert.ToDecimal(dt.Rows[i]["ClassPointPercent"]);
                    }
                    if ((dt.Rows[i]["ClassRechargePointRate"] != null) && (dt.Rows[i]["ClassRechargePointRate"].ToString() != ""))
                    {
                        item.LevelRechargePointRate = Convert.ToInt32(dt.Rows[i]["ClassRechargePointRate"]);
                    }
                    if ((dt.Rows[i]["LevellLock"] != null) && (dt.Rows[i]["LevellLock"].ToString() != ""))
                    {
                        if ((dt.Rows[i]["LevellLock"].ToString() == "1") || (dt.Rows[i]["LevellLock"].ToString().ToLower() == "true"))
                        {
                            item.LevellLock = true;
                        }
                        else
                        {
                            item.LevellLock = false;
                        }
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public List<Chain.Model.MemLevel> DataTableToList1(DataTable dt)
        {
            List<Chain.Model.MemLevel> list = new List<Chain.Model.MemLevel>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.MemLevel item = new Chain.Model.MemLevel();
                    if ((dt.Rows[i]["LevelID"] != null) && (dt.Rows[i]["LevelID"].ToString() != ""))
                    {
                        item.LevelID = int.Parse(dt.Rows[i]["LevelID"].ToString());
                    }
                    if ((dt.Rows[i]["LevelName"] != null) && (dt.Rows[i]["LevelName"].ToString() != ""))
                    {
                        item.LevelName = dt.Rows[i]["LevelName"].ToString();
                    }
                    if ((dt.Rows[i]["LevelPoint"] != null) && (dt.Rows[i]["LevelPoint"].ToString() != ""))
                    {
                        item.LevelPoint = int.Parse(dt.Rows[i]["LevelPoint"].ToString());
                    }
                    if ((dt.Rows[i]["LevelDiscountPercent"] != null) && (dt.Rows[i]["LevelDiscountPercent"].ToString() != ""))
                    {
                        item.LevelDiscountPercent = Convert.ToDecimal(dt.Rows[i]["LevelDiscountPercent"]);
                    }
                    if ((dt.Rows[i]["LevelPointPercent"] != null) && (dt.Rows[i]["LevelPointPercent"].ToString() != ""))
                    {
                        item.LevelPointPercent = Convert.ToDecimal(dt.Rows[i]["LevelPointPercent"]);
                    }
                    if ((dt.Rows[i]["LevelRechargePointRate"] != null) && (dt.Rows[i]["LevelRechargePointRate"].ToString() != ""))
                    {
                        item.LevelRechargePointRate = Convert.ToInt32(dt.Rows[i]["LevelRechargePointRate"]);
                    }
                    if ((dt.Rows[i]["LevellLock"] != null) && (dt.Rows[i]["LevellLock"].ToString() != ""))
                    {
                        if ((dt.Rows[i]["LevellLock"].ToString() == "1") || (dt.Rows[i]["LevellLock"].ToString().ToLower() == "true"))
                        {
                            item.LevellLock = true;
                        }
                        else
                        {
                            item.LevellLock = false;
                        }
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public int Delete(int LevelID)
        {
            return this.dal.Delete(LevelID);
        }

        public bool DeleteList(string LevelIDlist)
        {
            return this.dal.DeleteList(LevelIDlist);
        }

        public bool Exists(int LevelID)
        {
            return this.dal.Exists(LevelID);
        }

        public bool Exists(int LevelID, int Point)
        {
            return this.dal.Exists(LevelID, Point);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public List<Chain.Model.MemLevel> GetLevelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList1(list.Tables[0]);
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public DataSet GetLists(string strWhere)
        {
            return this.dal.GetLists(strWhere);
        }

        public int GetMaxId()
        {
            return this.dal.GetMaxId();
        }

        public Chain.Model.MemLevel GetModel(int LevelID)
        {
            return this.dal.GetModel(LevelID);
        }

        public List<Chain.Model.MemLevel> GetModelList(string strWhere)
        {
            DataSet lists = this.dal.GetLists(strWhere);
            return this.DataTableToList(lists.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public int Update(Chain.Model.MemLevel model)
        {
            if (this.Exists(model.LevelID, model.LevelPoint))
            {
                return -1;
            }
            return this.dal.Update(model);
        }
    }
}

